<?php
/**
 * @author jim
 * @date 2017/12/6/006
 * 资质认证
 */

namespace app\admin\controller;


use app\admin\model\CompanyAuthModel;

class Auth extends Common
{
    /**
     * 认证
     */
    public function index () {
        if (request()->isPost()) { // 处理表单提交
            if (input('post.id') > 0) {
                $data = $_POST;

                // 修改资质认证
                $file_business_license_pic = request()->file('business_license_pic');
                if ($file_business_license_pic) {
                    // 移动到框架应用根目录/public/uploads/ 目录下
                    $upload_info = $file_business_license_pic->move(config('upload_path'));
                    if($upload_info){
                        // 删除之前旧的图片
                        $oss_img_url = $this->aliyun($upload_info->getSaveName(),'bcp');
                        $data['business_license_pic'] = $oss_img_url;
                    }
                }

                $file_account_license_pic = request()->file('account_license_pic');
                if ($file_account_license_pic) {
                    // 移动到框架应用根目录/public/uploads/ 目录下
                    $upload_info = $file_account_license_pic->move(config('upload_path'));
                    if($upload_info){
                        $oss_img_url = $this->aliyun($upload_info->getSaveName(),'bcp');
                        $data['account_license_pic'] = $oss_img_url;
                    }
                }

                $data['lastupdatetime'] = date('Y-m-d H:i:s',time());

                $companyAuthModel = new CompanyAuthModel();
                $companyAuthModel->editData(array('id'=>input('post.id')),$data);

                $this->success("操作成功");

            } else  {
                // 检测是否存在，避免重复提交
                $where  = array();
                $where['cpid'] = session('company_id');
                $companyAuthModel = new CompanyAuthModel();
                $authInfo = $companyAuthModel->findData($where);
                if ($authInfo) {
                    $this->error("请勿重复提交");
                } else {
                    // 新增资质认证
                    $data = $_POST;

                    // 修改资质认证
                    $file_business_license_pic = request()->file('business_license_pic');
                    if ($file_business_license_pic) {
                        // 移动到框架应用根目录/public/uploads/ 目录下
                        $upload_info = $file_business_license_pic->move(config('upload_path'));
                        if($upload_info){
                            // 删除之前旧的图片
                            $oss_img_url = $this->aliyun($upload_info->getSaveName(),'bcp');
                            $data['business_license_pic'] = $oss_img_url;
                        }
                    }

                    $file_account_license_pic = request()->file('account_license_pic');
                    if ($file_account_license_pic) {
                        // 移动到框架应用根目录/public/uploads/ 目录下
                        $upload_info = $file_account_license_pic->move(config('upload_path'));
                        if($upload_info){
                            $oss_img_url = $this->aliyun($upload_info->getSaveName(),'bcp');
                            $data['account_license_pic'] = $oss_img_url;
                        }
                    }

                    $data['cpid'] = session('company_id');
                    $data['status'] = 0;
                    $data['addtime'] = date('Y-m-d H:i:s',time());
                    $data['lastupdatetime'] = date('Y-m-d H:i:s',time());

                    $companyAuthModel = new CompanyAuthModel();
                    $companyAuthModel->addData($data);

                    $this->success("操作成功");

                }

            }

        } else {
            // 获取资质认证数据
            $where  = array();
            $where['cpid'] = session('company_id');
            $companyAuthModel = new CompanyAuthModel();
            $authInfo = $companyAuthModel->findData($where);
            if ($authInfo) {
                $this->assign('authInfo',$authInfo);
            }

            return $this->fetch();
        }
    }


}